package com.pureut.production.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.production.domain.MaterialFinished;
import com.pureut.production.domain.dto.MaterialFinishedDto;
import com.pureut.production.domain.vo.MaterialMqVo;
import com.pureut.production.mapper.MaterialFinishedMapper;
import com.pureut.production.service.MaterialFinishedService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author: CM
 * @date: 2023/2/28  14:05
 */
@Service
public class MaterialFinishedServiceImpl extends ServiceImpl<MaterialFinishedMapper, MaterialFinished> implements MaterialFinishedService {

    @Resource
    MaterialFinishedMapper materialFinishedMapper;

    /**
     * 新增
     *
     * @param mq
     */
    @Override
    public void insertMaterialFinished(MaterialMqVo mq) {
        if (mq.getMaterialCode() != null) {
            MaterialFinished materialFinished1 = materialFinishedMapper.selectOne(new QueryWrapper<MaterialFinished>().lambda().eq(MaterialFinished::getMaterialCode, mq.getMaterialCode()));
            if (materialFinished1 == null) {
                MaterialFinished materialFinished = new MaterialFinished();
                materialFinished.setMaterialCode(mq.getMaterialCode())
                        .setMaterialName(mq.getMaterialName())
                        .setMaterialSpec(mq.getMaterialSpec())
                        .setMaterialPlace(mq.getMaterialPlace())
                        .setMaterialUnit(Integer.parseInt(mq.getMaterialUnit()))
                        .setMaterialHair(mq.getMaterialHair())
                        .setMaterialClosed(mq.getMaterialClosed())
                        .setMaterialGheavy(mq.getMaterialGheavy())
                        .setMaterialRatio(mq.getMaterialRatio())
                        .setMaterialNet(mq.getMaterialNet())
                        .setMaterialNetType(mq.getMaterialNetType())
                        .setMaterialType(mq.getMaterialType())
                        .setCategory(String.valueOf(mq.getMaterialCategory()))
                        .setMaterialInspection(mq.getMaterialInspection());
                save(materialFinished);
            }
        }
    }

    /**
     * 修改
     *
     * @param mq
     */
    @Override
    public void updateMaterialFinished(MaterialMqVo mq) {
        MaterialFinished materialFinished = materialFinishedMapper.selectOne(new QueryWrapper<MaterialFinished>().lambda().eq(MaterialFinished::getMaterialCode, mq.getMaterialCode()));
        if (materialFinished != null) {
            materialFinished.setMaterialCode(mq.getMaterialCode())
                    .setMaterialName(mq.getMaterialName())
                    .setMaterialSpec(mq.getMaterialSpec())
                    .setMaterialUnit(Integer.parseInt(mq.getMaterialUnit()))
                    .setMaterialHair(mq.getMaterialHair())
                    .setMaterialPlace(mq.getMaterialPlace())
                    .setMaterialClosed(mq.getMaterialClosed())
                    .setMaterialGheavy(mq.getMaterialGheavy())
                    .setMaterialRatio(mq.getMaterialRatio())
                    .setMaterialNet(mq.getMaterialNet())
                    .setMaterialNetType(mq.getMaterialNetType())
                    .setMaterialType(mq.getMaterialType())
                    .setCategory(String.valueOf(mq.getMaterialCategory()))
                    .setMaterialInspection(mq.getMaterialInspection());
            updateById(materialFinished);
        }
    }

    /**
     * 删除
     *
     * @param mq
     */
    @Override
    public void deletePublicGoods(MaterialMqVo mq) {
        if (mq.getMaterialCode() != null) {
            List<String> codes = Arrays.asList(mq.getMaterialCode().split(","));
            //根据物料编码查询对应物料
            List<MaterialFinished> materialFinished = materialFinishedMapper.selectList(new QueryWrapper<MaterialFinished>().lambda().in(MaterialFinished::getMaterialCode, codes));
            if (materialFinished.size() > 0) {
                removeByIds(materialFinished);
            }
        }
    }
}
